package cn.ren.service;

import cn.ren.dao.CheckItemDao;
import cn.ren.entity.PageResult;
import cn.ren.entity.QueryPageBean;
import cn.ren.pojo.CheckItem;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


/**
 * @program: itcast_health
 * @description:
 * @author: ren
 * @create: 2020-02-19 16:16
 **/
@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemDao checkItemDao;

    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<CheckItem> page = checkItemDao.queryByCondition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void del(int id) throws RuntimeException{
        //判断该检查项是否和检查组关联，关联的不能删
        long count = checkItemDao.findCountByCheckItemId(id);
        System.out.println("关联的检查组"+count);
        if (count > 0){
            throw new RuntimeException("当前检查项被引用，不能删");
        }else {
            checkItemDao.del(id);
        }
    }

    @Override
    public CheckItem findById(int id) {
        return checkItemDao.findById(id);
    }

    @Override
    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
    }
}
